package com.zhoumy.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;

import java.util.Arrays;

/**
 * @author zhoumy
 * @description 实例化处理器
 * @date 2024年6月27日10:22:43
 */
public class InstanceBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {

    public InstanceBeanPostProcessor() {
        System.out.println("第3步：这是InstantiationAwareBeanPostProcessorAdapter实现类(InstanceBeanPostProcessor)构造器！！");
    }

    //实例化bean之前调用
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        System.out.println("第5步：实例化"+beanClass.getName()+"之前调用,即调用"+beanClass.getName()+"类构造函数之前调用 ");
        return super.postProcessBeforeInstantiation(beanClass, beanName);
    }

    //实例化bean之后调用
    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        System.out.println("第7步：返回boolean,bean实例化后调用,并且返回false则不会注入属性");
        return super.postProcessAfterInstantiation(bean, beanName);
    }

    // 设置bean的某个属性时调用
    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        System.out.println("第8步：postProcessProperties,在属性注入之前调用；beanName = " + beanName + "；属性名集合 : " + Arrays.toString(pvs.getPropertyValues()));
        return super.postProcessProperties(pvs, bean, beanName);
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("第13步：postProcessBeforeInitialization(InstantiationAwareBeanPostProcessorAdapter实现类InstanceBeanPostProcessor的方法) ");
        return super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("第17步：postProcessAfterInitialization(InstantiationAwareBeanPostProcessorAdapter类的实现方法) ");
        return super.postProcessAfterInitialization(bean, beanName);
    }
}